package com.report.service.impl;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.convert.Convert;
import cn.zhxu.bs.BeanSearcher;
import cn.zhxu.bs.SearchResult;
import cn.zhxu.bs.operator.Equal;
import cn.zhxu.bs.util.MapBuilder;
import cn.zhxu.bs.util.MapUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.report.dto.request.SubscribeReq;
import com.report.dto.response.ReportRes;
import com.report.dto.response.SubscribeRes;
import com.report.entity.Report;
import com.report.entity.Subscribe;
import com.report.mapper.SubscribeMapper;
import com.report.service.IReportService;
import com.report.service.SubscribeService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author summer
 * @version 1.0
 * @date 2024/8/13 09:29
 */
@Service
@Transactional(rollbackFor = Exception.class)
@AllArgsConstructor
public class SubscribeServiceImpl extends ServiceImpl<SubscribeMapper, Subscribe> implements SubscribeService {

   private final BeanSearcher beanSearcher;

   private final IReportService reportService;


    @Override
    public List<SubscribeRes> subscribeLists(SubscribeReq subscribeReq) {
        Map<String, Object> build = MapUtils.builder().field(SubscribeRes::getMemberId, subscribeReq.getMemberId()).build();
        SearchResult<SubscribeRes> search = beanSearcher.search(SubscribeRes.class, build);
        List<SubscribeRes> dataList = search.getDataList();
        dataList.forEach(data->{
            Integer tagId = data.getTagId();
            Page<Report> page=new Page<>(1,5);
            LambdaQueryWrapper<Report> eq = Wrappers.lambdaQuery(Report.class).eq(Report::getIndustryId, tagId) .orderByDesc(Report::getCreateTime);
            page = reportService.page(page, eq);
            data.setReportResList(Convert.toList(ReportRes.class,page.getRecords()));
            data.setReportNum(page.getTotal());
        });
        return dataList;
    }


    @Override
    public void saveSubscribe(SubscribeReq subscribeReq) {
        LambdaQueryWrapper<Subscribe> eq = Wrappers.lambdaQuery(Subscribe.class).eq(Subscribe::getMemberId, subscribeReq.getMemberId());
        this.remove(eq);
        List<Integer> tagIds = subscribeReq.getTagIds();
        List<Subscribe> list= ListUtil.list(true);
        tagIds.forEach(tag->{
            Subscribe subscribe = new Subscribe();
            subscribe.setMemberId(subscribeReq.getMemberId());
            subscribe.setTagId(tag);
            list.add(subscribe);
        });
        this.saveBatch(list);
    }

    @Override
    public List<SubscribeRes> memberCheckTag(Integer memberId) {
        MapBuilder op = MapUtils.builder().field(SubscribeRes::getMemberId, memberId).op(Equal.class);
        SearchResult<SubscribeRes> search = beanSearcher.search(SubscribeRes.class, op.build());
        return search.getDataList();
    }

    @Override
    public void clearSearch(Integer memberId) {
         this.remove(Wrappers.lambdaQuery(Subscribe.class).eq(Subscribe::getMemberId, memberId));
    }
}
